<template>
  <el-form ref="elForm" :model="formData" size="medium" label-width="100px">
    <el-form-item label="用户名称">
      <el-input
        v-model="formData.fusername"
        placeholder="用户名称"
        clearable
        :style="{ width: '100%' }"
      ></el-input>
    </el-form-item>
    <el-form-item label="真实名称">
      <el-input
        v-model="formData.frealaname"
        placeholder="真实名称"
        clearable
        :style="{ width: '100%' }"
      ></el-input>
    </el-form-item>
    <el-form-item label="所属机构部门">
      {{ formData.fdeptname }}
    </el-form-item>
    <el-form-item label="账号到期时间">
      <el-date-picker
        v-model="formData.fduetime"
        type="date"
        placeholder="选择账号到期时间"
      >
      </el-date-picker>
    </el-form-item>
    <el-form-item label="联系电话">
      <el-input
        v-model="formData.fphone"
        placeholder="请输入联系电话"
        clearable
        :style="{ width: '100%' }"
      ></el-input>
    </el-form-item>
    <el-form-item label="邮箱">
      <el-input
        v-model="formData.femail"
        placeholder="请输入邮箱"
        clearable
        :style="{ width: '100%' }"
      ></el-input>
    </el-form-item>
    <el-form-item label="地址">
      <el-input
        type="textarea"
        v-model="formData.faddress"
        placeholder="请输入地址"
        :rows="2"
        :style="{ width: '100%' }"
      ></el-input>
    </el-form-item>
    <el-form-item size="large" style="    height: 60px;">
      <el-button type="primary" @click="submitForm">提交</el-button>
      <el-button @click="resetForm">重置</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { selectOne, SaveData } from '@/api/tappUser'
import moment from 'moment'
export default {
  components: {},
  props: ['datainfo'],
  data() {
    return {
      formData: {
        fusername: undefined,
        frealaname: undefined,
        fdeptname: undefined,
        fduetime: undefined,
        fphone: undefined,
        faddress: undefined,
        femail: undefined,
        fid: undefined,
        fdeptcode: undefined
      }
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    if (this.datainfo !== undefined && this.datainfo !== null) {
      if (this.datainfo.fid !== undefined && this.datainfo.fid !== null) {
        selectOne(this.datainfo.fid).then(res => {
          if (res.code === 0) {
            this.formData.fusername = res.data.fusername
            this.formData.fdeptname = this.datainfo.fdeptname
            this.formData.fdeptcode = this.datainfo.fdeptcode
            this.formData.frealaname = res.data.frealaname
            this.formData.fduetime = res.data.fduetime
            this.formData.fphone = res.data.fphone
            this.formData.faddress = res.data.faddress
            this.formData.femail = res.data.femail
            this.formData.fid = res.data.fid
            this.formData.fdeptcode = res.data.fdeptcode
          }
        })
      } else {
        Object.assign(this.formData, this.datainfo)
      }
    }
  },
  methods: {
    submitForm() {
      if (
        this.formData.fusername === '' ||
        this.formData.fusername === undefined
      ) {
        this.$message.success('请输入用户名称')
        return
      }
      if (
        this.formData.fusername === '' ||
        this.formData.fusername === undefined
      ) {
        this.$message.success('请输入用户真实名称')
        return
      }
      if (this.formData.fphone === '' || this.formData.fphone === undefined) {
        this.$message.success('请输入联系电话')
        return
      }
      if (
        this.formData.fduetime === '' ||
        this.formData.fduetime === undefined
      ) {
        this.$message.success('请输入到期时间')
        return
      }
      this.formData.fduetime = moment(this.formData.fduetime).format(
        'YYYY-MM-DD HH:mm:ss'
      )
      SaveData(this.formData).then(res => {
        if (res.code === 0) {
          this.formData.fid = res.data.fid
          this.$emit('savedata', this.formData)
          this.$message.success('保存成功！')
        }
      })
    },
    resetForm() {
      this.formData.fusername = undefined
      this.formData.frealaname = undefined
      this.formData.fduetime = undefined
      this.formData.fphone = undefined
      this.formData.faddress = undefined
      this.formData.femail = undefined
    }
  }
}
</script>
